
function getGames(game)
{
	var http = getHttpReq();
	http.open("GET", "lib/ajax.php?call=getgames&game=" + game, true);
	http.onreadystatechange = function()
	{
		
		if(http.readyState == 4)// && http.status == 200)
		{
			var xmldoc = http.responseText;
			//alert(xmldoc);
			if(xmldoc.indexOf("@") < 0)
			{
				var div = document.getElementById("gameList");
				div.appendChild(document.createTextNode(xmldoc));
				return;
			}
			makeTable(game);
		    var i = 0;
		    var row;
		    var rowArray = makeArray(xmldoc, "@");
		    row = rowArray[i++];
		    while ((row != null || row != "") && i < 10)
		    {
			    var temp = makeArray(row, ";");
			    if(temp != null)
					addRow("gip", temp);
			    row = rowArray[i++];
		    }           
		}
	}
	http.send(null);

}

function makeArray(str, dil)
{
	var index;
	var ret = new Array();
	if(str.indexOf(dil) == 0)
		index=1;
	else if (str.indexOf(dil) < 0)
		return null;
	else    
		index = 0;
	var nindex;
	while((nindex = str.indexOf(dil, index))>0)
	{
		ret.push(str.substring(index,nindex));
		index = nindex+1;
	}
  
	if(str.lastIndexOf(dil) != (str.length-1))
		ret.push(str.substring(index));
	return ret;
}

function makeTable(game)
{
	var div = document.getElementById("gameList");
	var table = document.createElement("table");
	table.setAttribute("id", "gip");
	var row = table.insertRow();
	row.className = 'titlerow';
	row.insertCell(0).innerHTML = "Table Number";
	row.insertCell(1).innerHTML = "Game";
	row.insertCell(2).innerHTML = "Status";
	row.insertCell(3).innerHTML = "Total Seats";
	row.insertCell(4).innerHTML = "Seats Available";
	div.innerHTML = "<h3>" + game + "</h3> <a href=\"games/" + game + ".php\" target=\"_blank\"> new game</a>";
	div.appendChild(table);
	//alert(table.getAttribute("id"));
}

function addRow(tablename, arr)
{
	var i=0;
	var temp;
	var table = document.getElementById(tablename);
	var lastRow = table.rows.length;
	var newRow = table.insertRow(lastRow);
	if (lastRow%2)
		newRow.className = 'evenrow';
	else
		newRow.className = 'oddrow';
	while((temp = arr[i]) != null && i < 5)
	{
		var col = newRow.insertCell(i++);
		if(i == 1)
		{
			col.innerHTML = "<a href='joingame.php?table=" + temp + "' target=\"_blank\">" + temp + "</a>";
		}
		else
		{
			col.innerHTML = temp;
		}
	}
}


//Function to return HTTP Request object for other java script functions to use.
function getHttpReq()
{
	var xmlHtt;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHtt=new XMLHttpRequest();
		if (xmlHtt.overrideMimeType) 
		{
			xmlHtt.overrideMimeType('text/xml');
		}
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHtt=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHtt=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHtt;
}




